import { reactive, Ref, watchEffect, ref } from 'vue'
import {
  totalTenantPostNumber,
  findALLNumEducation,
  findAllGradeLevelInterveneNumEducation,
  findAllGradeNoTestNumEducation,
  findALLTestNumEducation,
  findFinishNoTestNumEducation,
  findFinishTestNumEducation,
  findGradeLevelInterveneNumEducation,
  findGradeLevelNum1Education,
  findGradeLevelNumEducation,
  findLevelAllNumEducation,
  findLevelAreaNumEducation,
  findLevelNumEducation,
  findLevelSexNumEducation,
  findSexLevelNumEducation,
  findNoReleaseFinishTestNumEducation,
  findReleaseFinishTestNumEducation,
} from '../api'
import { NumData, TotalData } from '../types'
import { getList } from '/@/api/system/dict'
import { Dict } from '/@/types/Dictionary'
import { Half } from '/@/types/Half'
import { Project } from '/@/types/Project'
import { Term } from '/@/types/Term'
import { useLoginer } from '/@/use/loginer'

function getData(response: any) {
  try {
    return response.data.data || []
  } catch (error) {
    console.error(error)
    return []
  }
}

export function useBoarDataEducation({
  project,
  yearly,
  half,
  term,
}: {
  project: Ref<Project | null>
  yearly: Ref<string>
  half: Ref<Half | null>
  term: Ref<Term | null>
}) {
  const { loginer } = useLoginer()
  const numData = reactive<NumData>({
    allNum: [],
    allTestNum: [],
    allGradeLevelInterveneNum: [],
    finishTestNum: [],
    finishNoTestNum: [],
    gradeLevelInterveneNum: [],
    gradeLevelNum: [],
    levelAllNum: [],
    levelAreaNum: [],
    levelNum: [],
    levelSexNum: [],
    sexLevelNum: [],
    allGradeNoTestNum: [],
    gradeLevelNum1: [],
    levelData: [],
    abnormalLevelNum: [],
    releaseFinishTestNumEducation: [],
    noReleaseFinishTestNumEducation: [],
  })
  const totalData = reactive<TotalData>({
    teacherTotal: 0,
    studentTotal: 0,
  })

  const loading = ref(false)
  const getBoarData = async ({
    yearly,
    tenantId,
    projectId,
    half,
    termId,
  }: {
    yearly: string
    tenantId: string
    projectId: string
    half: Half | null
    termId: string
  }) => {
    const params = { tenantId, projectId, yearly, half, termId }

    const promiseList = [
      findALLNumEducation(params).then(
        (response) => (numData.allNum = getData(response))
      ),
      findALLTestNumEducation(params).then(
        (response) => (numData.allTestNum = getData(response))
      ),
      findAllGradeLevelInterveneNumEducation({
        tenantId,
        projectId,
      }).then(
        (response) => (numData.allGradeLevelInterveneNum = getData(response))
      ),
      findFinishNoTestNumEducation(params).then(
        (response) => (numData.finishNoTestNum = getData(response))
      ),
      findFinishTestNumEducation(params).then(
        (response) => (numData.finishTestNum = getData(response))
      ),
      findGradeLevelInterveneNumEducation({
        tenantId,
        projectId,
      }).then(
        (response) => (numData.gradeLevelInterveneNum = getData(response))
      ),
      findGradeLevelNumEducation(params).then(
        (response) => (numData.gradeLevelNum = getData(response))
      ),
      findLevelAllNumEducation(params).then(
        (response) => (numData.levelAllNum = getData(response))
      ),
      findLevelAreaNumEducation(params).then(
        (response) => (numData.levelAreaNum = getData(response))
      ),
      findLevelNumEducation(params).then(
        (response) => (numData.levelNum = getData(response))
      ),
      findLevelSexNumEducation(params).then(
        (response) => (numData.levelSexNum = getData(response))
      ),
      findSexLevelNumEducation(params).then(
        (response) => (numData.sexLevelNum = getData(response))
      ),
      findAllGradeNoTestNumEducation(params).then(
        (response) => (numData.allGradeNoTestNum = getData(response))
      ),
      findGradeLevelNum1Education(params).then(
        (response) => (numData.gradeLevelNum1 = getData(response))
      ),
      // getNewUserInfo({}),
      totalTenantPostNumber({ tenantId, category: 2 }).then(
        (response) => (totalData.teacherTotal = getData(response))
      ),
      totalTenantPostNumber({ category: 4 }).then(
        (response) => (totalData.studentTotal = getData(response))
      ),
      getList(1, 1000, { code: 'warning_level' }).then((response) => {
        const levelData: Dict[] = getData(response)
        numData.levelData = []
        levelData[0]?.children?.map((vv) => {
          if (vv.dictKey != 0) {
            numData.levelData.push(vv)
          }
        })
      }),
      findReleaseFinishTestNumEducation(params).then((res) => {
        numData.findReleaseFinishTestNum = res.data.data
      }),
      findNoReleaseFinishTestNumEducation(params).then((res) => {
        numData.findNoReleaseFinishTestNum = res.data.data
      }),
    ]

    const responseList = await Promise.all(promiseList)

    loading.value = true
  }

  watchEffect(() => {
    if (!loginer.value?.tenantId) {
      return
    }
    const projectId = project.value?.projectId || ''

    const params = {
      yearly: yearly.value,
      tenantId: loginer.value.tenantId,
      projectId,
      half: half.value,
      termId: term.value?.termId || '',
    }

    getBoarData(params)
  })

  return { numData, totalData, loading }
}
